package com.groenlid.mobileapp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

import com.groenlid.mobileapp.Utils.Base64;
import com.groenlid.mobileapp.Utils.RestClient;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MobApp extends Activity {
    private static final int ACTIVITY_CREATE = 0;
    
    public static String TAG = "FATMAN";

    EditText username, password;
    RestClient client;
    SharedPreferences settings;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	Log.i(TAG,"Starting program");
        super.onCreate(savedInstanceState);
        
        client = new RestClient(this.getApplicationContext());
        settings = getSharedPreferences("FATBOY",MODE_PRIVATE);
        
        // First we try to login
        if(client.isLoggedIn(settings))
        	goToMenu();
        setContentView(R.layout.main);
        Button login = (Button) findViewById(R.id.btnlogin);
        username = (EditText) findViewById(R.id.editText1);
        password = (EditText) findViewById(R.id.editText2);
    }
    
    private String getCredentials() 
    {
    	String user = username.getText().toString();
    	String pass = password.getText().toString();
    	return(Base64.encodeBytes((user+":"+pass).getBytes()));
    }

    private void goToMenu() 
    {
    	Intent i = new Intent(this,MainMenu.class);
    	startActivity(i);
    }
    
    /**
     * Logging in to RPC
     * @param view
     */
    public void login(View view){
    	
    	String username = "groenlid@gmail.com";
    	String password = "test";
    	
    	
    	if(client.login(username,password)){
            	// Login Here
            	SharedPreferences.Editor editor = settings.edit();
            	
            	editor.putString("username", username);
            	editor.putString("password", password);
            	
            	editor.commit();
            	
            	Toast.makeText(this, "Logged in", Toast.LENGTH_LONG).show();
            	
            	goToMenu();
            }
            else{
            	//Unauthorized
            	Toast.makeText(this, "WRONG USERNAME OR PASSWORD", Toast.LENGTH_LONG).show();
            }
    	
    	
    }
    
    public void goRunning(View view){
    	Intent i = new Intent(this, Running.class);
    	startActivity(i);
    }
}